package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.Utils.SubmitValueVerification;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.Style;
import com.jzg.jcptadmin.data.vo.StyleCategory;
import com.jzg.jcptadmin.data.vo.StyleList;
import com.jzg.jcptadmin.viewinterface.StyleInterface;

import java.util.ArrayList;
import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by wangyd on 15/12/23.
 * 品牌列表逻辑
 */
public class StylePresenter extends BasePresenter<StyleInterface> {
    private final DataManager mDataManager;
    private Subscription mSubscription;
    private StyleInterface mView;

    public StylePresenter(DataManager instance) {
        super();
        mDataManager = instance;
    }

    @Override
    public void attachView(StyleInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    /**
     * 加载车型数据
     */
    public void loadStyle() {
        checkViewAttached();
        Map<String, String> params = mView.getStyleParams();
        if (SubmitValueVerification.isEmpty(params)) return;
        if (!AppContext.isNetWork) return;


        mSubscription = mDataManager.loadStyle(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new Observer<StyleList>() {
                    @Override
                    public void onCompleted() {
//                        Logger.i("loadStyle onCompleted.");
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e != null&& mView!=null) {
                            mView.showError(e.toString());
                        }
                    }

                    @Override
                    public void onNext(StyleList styleList) {
//                        Logger.i(styleList.toString());
                        if (styleList.getStatus() == Constant.SUCCESS) {
                            ArrayList<StyleCategory> yearGroupList = styleList.getYearGroupList();
                            ArrayList<Style> styles = new ArrayList<Style>();
                            ArrayList<String> groupNames = new ArrayList<String>();
                            for (StyleCategory category : yearGroupList) {
                                Style group = new Style();
                                String groupName = category.getYear();
                                groupNames.add(groupName);
                                group.setName(groupName);
                                group.setFontColor(mView.getDefaultFontColor());
                                // 添加标题到列表
                                styles.add(group);
                                // 添加所有选项到列表
                                for (Style style : category.getStyleList()) {
                                    style.setFontColor(mView.getDefaultFontColor());
                                    styles.add(style);
                                }
                            }
                            mView.setStyles(styles);
                            mView.setStylesGroupNames(groupNames);
                            mView.showStyle();
                        } else {
                            mView.showError(styleList.getMsg());
                        }

                    }
                });

    }

    /**
     * 保存车型信息
     *
     * @param style
     * @return
     */
//    public void saveStyleInfo(AppContext context, Style style) {
//        context.setFullName(style.getFullName());
//        context.setStyleId(String.valueOf(style.getId()));
//    }
}

